/*
 * Copyright (C) 2016  Nicola Spanti (RyDroid) <dev@nicola-spanti.info>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */


#include "PlanetWars2dRT-SDL2/specific/controller/game_playing_data.h"
#include <SDL2/SDL_timer.h>


EXTERN_C_BEGIN


bool
game_playing_data_sdl_init_first_time_only(game_playing_data_sdl* data)
{
  if(data == NULL)
    {
      return false;
    }
  
  data->main_window_renderer = NULL;
  data->main_window          = NULL;
  data->time_ms_per_frame    =   40;
  data->time_ms_last_frame   = SDL_GetTicks();
  data->frame_per_second     =   -1;
  return true;
}

struct game_playing_data_sdl
game_playing_data_sdl_create(void)
{
  struct game_playing_data_sdl data;
  game_playing_data_sdl_init_first_time_only(&data);
  return data;
}


unsigned short
game_playing_data_sdl_get_max_time_ms_per_frame(const game_playing_data_sdl* data)
{
  return data->time_ms_per_frame;
}

float
game_playing_data_sdl_get_frame_per_second_max(const game_playing_data_sdl* data)
{
  return data == NULL ? -1 : 1000.0 / data->time_ms_per_frame;
}

float
game_playing_data_sdl_get_frame_per_second_current(const game_playing_data_sdl* data)
{
  return data == NULL ? -1 : data->frame_per_second;
}

bool
game_playing_data_sdl_set_max_time_ms_per_frame(game_playing_data_sdl* data,
						unsigned short value)
{
  if(data == NULL)
    {
      return false;
    }
  data->time_ms_per_frame = value;
  return true;
}

bool
game_playing_data_sdl_set_frame_per_second_max(game_playing_data_sdl* data,
					       float value)
{
  if(data == NULL || value < 0)
    {
      return false;
    }
  data->time_ms_per_frame = 1000 / value;
  return true;
}


EXTERN_C_END
